
import java.sql.*;
import java.util.Vector;

import _tema6.Respuesta;

public class RespuestasDAO {
	
	public Object[] obtenerSPs() {
		Data data = new Data();
		data.abrirConexion();
		try {
  
			Statement stmt = data.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(IJDBC.SPS);
			Vector v = new Vector();
			while (rs.next()) {
				v.add(rs.getString("name"));
			}
			Object[] resp = new Object[v.size()];
			for (int i=0; i < v.size(); i++) {
				resp[i] = v.elementAt(i);
			}
			return resp;
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
			return null;
		}
	}
	
	public Object[] obtenerTitulos(String sp, int zona, int categoria) {
		Data data = new Data();
		data.abrirConexion();
		try {
			CallableStatement stmt = data.getConnection().prepareCall("{call " + sp + "(?,?)}");
			stmt.setInt(1, zona);
			stmt.setInt(2, categoria);
			ResultSet rs = stmt.executeQuery();
			Object[] resp = new Object[rs.getMetaData().getColumnCount()];
			for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
				resp[i - 1] = rs.getMetaData().getColumnName(i);
			}
			return resp;
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
			return null;
		}
	}
	
	public Object[][] obtenerDatos(String sp, int zona, int categoria) {
		Data data = new Data();
		data.abrirConexion();
		try {
			CallableStatement stmt = data.getConnection().prepareCall("{call " + sp + "(?,?)}");
			stmt.setInt(1, zona);
			stmt.setInt(2, categoria);
			ResultSet rs = stmt.executeQuery();
			
			Vector f = new Vector();
			
			int i = 0;
			while (rs.next()) {
				i++;
				Vector c= new Vector();
				f.add(c);
				for (int j = 1; j <= rs.getMetaData().getColumnCount(); j++) {
					c.add(rs.getString(j));
					//resp[i - 1][j - 1] = rs.getString(j);
				}
			}
			Object[][] resp = new Object[f.size()][rs.getMetaData().getColumnCount()];
			for (int ii = 0; ii < f.size(); ii++)
				for (int jj = 0; jj < rs.getMetaData().getColumnCount(); jj++)
					resp[ii][jj] = ((Vector)f.elementAt(ii)).elementAt(jj);
			return resp;
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
			return null;
		}
	}

	
}